package Project;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
/**
 * This is a class contains all the graphics of upgrade menu
 * of weapon. 
 * 
 * @author	Brian Wetherbee
 * 			Ryan McNamara
 * 			Zeng Yang
 *
 */
public class UpgradeMenu extends Menu {
	/*@ public invariant Main.UI = this; @*/
	private UI prevUI;
	
	public UpgradeMenu() {
		super(Constants.NUM_WEAPON_ATTRIBUTES);
		// TODO Auto-generated constructor stub
	}
	
	public UpgradeMenu(UI prevUI)
	{
		this();
		this.prevUI = prevUI;
		
		initComponents();
	}
	
	private void initComponents() {
		addMouseListener(this);
		addKeyListener(this);
		repaint();
	}
	
	public void paint(Graphics g) {
		super.paint(g);
		// Draw background
		g.setColor(new Color(64,64,64));
		g.fillRect(0, 0, Constants.width, Constants.height);
		
		// Draw sub-menu rectangles.
		g.setColor(new Color(128,128,128));
		g.drawRect(0, 0, Constants.width, 64);
		g.drawRect(16, 80, Constants.width - 32, 258);
		g.drawRect(16, 354, Constants.width - 32, 258);
		g.drawRect(16, 628, Constants.width - 32, 258);
		g.drawRect(0, Constants.height - 64, (int) (.75 * Constants.width), 64);
		g.drawRect((int)(.75 * Constants.width), Constants.height - 64, (int) (.25 * Constants.width), 64);
		
		// Draw Header/Footer Text
		g.setColor(Color.WHITE);
		FontMetrics f = g.getFontMetrics(new Font("DialogInput",1,48));
		int stringX = f.stringWidth("Upgrade Menu");
		g.setFont(new Font("DialogInput",1,48));
		g.drawString("Upgrade Menu", Constants.width / 2 - stringX / 2, 44);
		Integer money = new Integer((int) SaveFile.getCurrentFile().getMoney());
		g.drawString("$ " + money.toString(),(int) (.75 * Constants.width + 4),Constants.height - 16);
		
		g.setFont(new Font("DialogInput",1,24));
		g.drawString("Status", 1150,112);
		g.drawString("Price", 1450,112);
		g.drawString("Status", 1150,386);
		g.drawString("Price", 1450,386);
		g.drawString("Status", 1150,660);
		g.drawString("Price", 1450,660);
		
		// Draw Weapon Name Text
		g.setFont(new Font("DialogInput",1,24));
		g.drawString("Awso-Gun",32,209);
		g.drawString("Weapon 2",32,487);
		g.drawString("Weapon 3",32,765);
		
		// Draw Weapon Attribute Text
		g.drawString("Damage",Constants.width / 2, 176);
		g.drawString("Cooldown",Constants.width / 2, 238);
		g.drawString("Speed", Constants.width / 2, 302);
		
		g.drawString("Attribute 1",Constants.width / 2, 450);
		g.drawString("Attribute 2",Constants.width / 2, 512);
		g.drawString("Attribute 3", Constants.width / 2, 574);
		
		g.drawString("Attribute 1",Constants.width / 2, 724);
		g.drawString("Attribute 2",Constants.width / 2, 786);
		g.drawString("Attribute 3", Constants.width / 2, 848);
		
		// Draw Attribute Number and price
		drawDetails(g);
	
		// Draw Selector
		drawSelector(g);
	}

	private void drawDetails(Graphics g) {
		ArrayList<Integer> weaponStat = SaveFile.getCurrentFile().getWeaponUpgradeStatus().get(0);
		if(weaponStat.get(0) == 0) {
			g.drawString("3",1150,176);
		} else if(weaponStat.get(0) == 1) {
			g.drawString("5",1150,176);
		} else if(weaponStat.get(0) == 2) {
			g.drawString("7",1150,176);
		} else if(weaponStat.get(0) == 3) {
			g.drawString("9",1150,176);
		} else if(weaponStat.get(0) == 4) {
			g.drawString("11",1150,176);
		} else if(weaponStat.get(0) == 5) {
			g.drawString("15",1150,176);
		}
		
		if(weaponStat.get(1) == 0) {
			g.drawString("300",1150,238);
		} else if(weaponStat.get(1) == 1) {
			g.drawString("265",1150,238);
		} else if(weaponStat.get(1) == 2) {
			g.drawString("230",1150,238);
		} else if(weaponStat.get(1) == 3) {
			g.drawString("195",1150,238);
		} else if(weaponStat.get(1) == 4) {
			g.drawString("160",1150,238);
		} else if(weaponStat.get(1) == 5) {
			g.drawString("90",1150,238);
		}

		if(weaponStat.get(2) == 0) {
			g.drawString("0.9",1150,302);
		} else if(weaponStat.get(2) == 1) {
			g.drawString("1.0",1150,302);
		} else if(weaponStat.get(2) == 2) {
			g.drawString("1.1",1150,302);
		} else if(weaponStat.get(2) == 3) {
			g.drawString("1.2",1150,302);
		} else if(weaponStat.get(2) == 4) {
			g.drawString("1.3",1150,302);
		} else if(weaponStat.get(2) == 5) {
			g.drawString("1.5",1150,302);
		}
		
		if(weaponStat.get(0) < Constants.MAX_UPGRADE_STATUS.get(0).get(0)) {
			g.drawString(Constants.UPGRADE_COSTS.get(0).get(0).get(weaponStat.get(0)).toString(), 1450, 176);
		} else {
			g.drawString("N/A",1450,176);
		}
		
		if(weaponStat.get(1) < Constants.MAX_UPGRADE_STATUS.get(0).get(1)) {
			g.drawString(Constants.UPGRADE_COSTS.get(0).get(1).get(weaponStat.get(1)).toString(), 1450, 238);
		} else {
			g.drawString("N/A",1450,238);
		}
		
		if(weaponStat.get(2) < Constants.MAX_UPGRADE_STATUS.get(0).get(2)) {
			g.drawString(Constants.UPGRADE_COSTS.get(0).get(2).get(weaponStat.get(2)).toString(), 1450, 302);
		} else {
			g.drawString("N/A",1450,302);
		}
		
		g.drawString("N/A",1150,450);
		g.drawString("N/A",1150,512);
		g.drawString("N/A",1150,574);
		
		g.drawString("N/A",1450,450);
		g.drawString("N/A",1450,512);
		g.drawString("N/A",1450,574);
		
		g.drawString("N/A",1150,724);
		g.drawString("N/A",1150,786);
		g.drawString("N/A",1150,848);
		
		g.drawString("N/A",1450,724);
		g.drawString("N/A",1450,786);
		g.drawString("N/A",1450,848);
	}
	
	private void drawSelector(Graphics g) {
		g.setColor(new Color(64,64,64));
		
		g.drawRect(Constants.width / 2 - 4, 157, 164,24);
		g.drawRect(Constants.width / 2 - 4, 219, 164,24);
		g.drawRect(Constants.width / 2 - 4, 281, 164,24);
		
		g.setColor(new Color(224,224,224));
		if(getOption() == 0) {
			g.drawRect(Constants.width / 2 - 4, 157, 164,24);
		} else if(getOption() == 1) {
			g.drawRect(Constants.width / 2 - 4, 219, 164,24);
		} else if(getOption() == 2) {
			g.drawRect(Constants.width / 2 - 4, 281, 164,24);
		}
	}
	
	@Override
	public void select() {
		ArrayList<ArrayList<Integer>> weaponStat = SaveFile.getCurrentFile().getWeaponUpgradeStatus();
		Integer maxLevel = Constants.MAX_UPGRADE_STATUS.get(getOption() / 3).get(getOption() % 3);
		Integer currLevel = weaponStat.get(getOption() / 3).get(getOption() % 3);
		if(currLevel < maxLevel) {
			Integer price = Constants.UPGRADE_COSTS.get(getOption() / 3).get(getOption() % 3).get(weaponStat.get(getOption() / 3).get(getOption() % 3));
			if(SaveFile.getCurrentFile().getMoney() >= price) {
				ArrayList<Integer> modify = weaponStat.get(getOption() / 3);
				modify.set(getOption() % 3, modify.get(getOption() % 3) + 1);
				weaponStat.set(getOption() / 3, modify);
				
				SaveFile.getCurrentFile().setMoney(SaveFile.getCurrentFile().getMoney() - price);
			}
		}
		
		repaint();
	}
	
	public void exitMenu() {
		Main.setCurrentUI(prevUI);
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyPressed(KeyEvent e) {
		if(e.getKeyCode() == Constants.controlUp) {
			decreaseHighlight();
			repaint();
		}
		if(e.getKeyCode() == Constants.controlDown) {
			increaseHighlight();
			repaint();
		}
		if(e.getKeyCode() == Constants.controlConfirm) {
			select();
		}
		if(e.getKeyCode() == Constants.controlCancel) {
			exitMenu();
		}
		if(e.getKeyCode() == Constants.controlQuit) {
			System.exit(0);
		}
		
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	
}
